const { getUserId } = require('../../utils')

const user = {
  async user (parent, { email, id }, ctx, info) {
    if (!email && !id) {
      throw new Error('need unique key for query user')
    }
    return ctx.db.query.user({ where: { email, id } }, info)
  },
  async users (parent, { first, skip, after, hobbyIds }, ctx, info) {
    const userId = getUserId(ctx)
    const where = {}
    where.id_not = userId
    if (hobbyIds) {
      where.hobbies_some = {
        id_in: hobbyIds
      }
    }
    return ctx.db.query.users({
      first,
      skip,
      after,
      where
    }, info)
  },
  async usersConnection (parent, { first, skip, after, hobbyIds }, ctx, info) {
    const userId = getUserId(ctx)
    return ctx.db.query.usersConnection({
      first,
      skip,
      after,
      where: {
        id_not: userId/*,
        hobbies_some: {
          id_in: hobbyIds
        } */
      }
    }, info)
  }
}

module.exports = { user }
